/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.EjercicioTarea.procesos.Encriptar;
import com.proyecto.datos.EntityPregunta;
import com.proyecto.modelado.Pregunta;
import com.proyecto.modelado.Usuario;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author mario
 */
public class frmMantenimientoUsuario extends javax.swing.JInternalFrame {

    Usuario usuario;
    Pregunta pre;
    ArrayList arryPre;
    int id_pregunta;
  
    //LLENANDO COMBO
    
     public void CargarComboPlan(){
        
        
        arryPre = new ArrayList();
        pre = new Pregunta();
        arryPre = pre.ObtenerPreguntas();
        EntityPregunta entPre = new EntityPregunta();;
        JList list = new JList();
        DefaultListModel listModel = new DefaultListModel();
        listModel.addElement("[SELECCIONE]");
        if (arryPre != null) {
            for (int i = 0; i < arryPre.size(); i++) {
                entPre = (EntityPregunta) arryPre.get(i);
                listModel.addElement(entPre.getPregunta());
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Materia", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        }
        list.setModel(listModel);
        this.jcmbPregunta.setModel(new DefaultComboBoxModel(listModel.toArray()));
        this.jcmbPregunta.setSelectedIndex(0);

    }
     
     public void limpiarFrm(){
         this.jtxtPass.setText("");
         this.jtxtRespuesta.setText("");
         this.jtxtUsuario.setText("");
     }
    
    
    public frmMantenimientoUsuario() {
        initComponents();
        this.setTitle("Agregar un nuevo usuario");
        CargarComboPlan();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtxtUsuario = new javax.swing.JTextField();
        jtxtPass = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jcmbPregunta = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jtxtRespuesta = new javax.swing.JTextField();
        jbtnAgregar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setClosable(true);

        jLabel1.setText("Usuario:");

        jLabel2.setText("Contraseña:");

        jLabel3.setText("Preguta de seguridad:");

        jcmbPregunta.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcmbPregunta.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbPreguntaItemStateChanged(evt);
            }
        });

        jLabel4.setText("Respuesta:");

        jbtnAgregar.setIcon(new javax.swing.ImageIcon("/home/mario/Dropbox/Sistema de laboratorio/Imagenes/agregar.jpg")); // NOI18N
        jbtnAgregar.setText("Agregar");
        jbtnAgregar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnAgregarMouseClicked(evt);
            }
        });

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCancelarMouseClicked(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Ubuntu", 2, 10)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(193, 49, 49));
        jLabel5.setText("Tener en cuenta espacios, mayúsculas");

        jLabel6.setFont(new java.awt.Font("Ubuntu", 2, 10)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(193, 49, 49));
        jLabel6.setText("o caracteres especiales.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jcmbPregunta, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jtxtRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtxtUsuario, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtPass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6))
                .addContainerGap(140, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcmbPregunta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtxtRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAgregar)
                    .addComponent(jbtnCancelar))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnAgregarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnAgregarMouseClicked
        try{
        String usu=this.jtxtUsuario.getText().trim();
        String contrasenna=this.jtxtPass.getText().trim();
        int pregunta=this.id_pregunta;
        String respuesta=this.jtxtRespuesta.getText().trim();
        
        usuario= new Usuario();
        Encriptar en = new Encriptar();
        String nuevaContrasenna=en.encriptar(contrasenna);
        
        String msj=usuario.RegitsrarUsuario(usu, nuevaContrasenna, pregunta, respuesta);
        JOptionPane.showMessageDialog(rootPane, msj, "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        
        limpiarFrm();
        CargarComboPlan();
        
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_jbtnAgregarMouseClicked

    private void jbtnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCancelarMouseClicked
       this.dispose();
    }//GEN-LAST:event_jbtnCancelarMouseClicked

    private void jcmbPreguntaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbPreguntaItemStateChanged
        ArrayList arry2 = new ArrayList();
        arry2 = pre.ObtenerPreguntas();
        EntityPregunta entPre = new EntityPregunta();
        for (int i = 0; i < arry2.size(); i++) {
            entPre = (EntityPregunta) arry2.get(i);
            if (this.jcmbPregunta.getSelectedItem().equals(entPre.getPregunta())) {
                id_pregunta = entPre.getId();
            }
        }
    }//GEN-LAST:event_jcmbPreguntaItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbtnAgregar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JComboBox jcmbPregunta;
    private javax.swing.JPasswordField jtxtPass;
    private javax.swing.JTextField jtxtRespuesta;
    private javax.swing.JTextField jtxtUsuario;
    // End of variables declaration//GEN-END:variables
}
